package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.carouselimage.domain.Carouselimage;
import com.zhentao.carouselimage.dto.CarouseLimageDto;
import com.zhentao.common.dtos.PageResponseResult;
import com.zhentao.common.dtos.ResponseResult;
import com.zhentao.common.enums.AppHttpCodeEnum;
import com.zhentao.file.service.FileStorageService;
import com.zhentao.service.CarouselimageService;
import com.zhentao.mapper.CarouselimageMapper;
import lombok.SneakyThrows;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;


/**
* @author 86188
* @description 针对表【carouselimage】的数据库操作Service实现
* @createDate 2024-08-30 22:30:58
*/
@Service
public class CarouselimageServiceImpl extends ServiceImpl<CarouselimageMapper, Carouselimage>
    implements CarouselimageService{

    @Override
//    @Cacheable(value = "carouselimage:list")
    public ResponseResult indexHomeList() {
        List<Carouselimage> list = list(Wrappers.<Carouselimage>lambdaQuery()
                .eq(Carouselimage::getStatus,1));
        return ResponseResult.okResult(list);
    }

    /**
     * 后端 轮播图查询展示
     * @return
     */
    @Override
    public ResponseResult ClListAll(CarouseLimageDto dto) {
        // 设置分页
        Page<Carouselimage> carouselimagePage = new Page<>(dto.getOffset(),dto.getPagesize());
                                                                // 设置查询条件
        Page<Carouselimage> page = this.page(carouselimagePage,Wrappers.<Carouselimage>lambdaQuery().ne(Carouselimage::getStatus,3).orderByAsc(Carouselimage::getPosition));
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setCurrentPage((int) page.getCurrent());
        pageResponseResult.setSize((int) page.getSize());
        pageResponseResult.setTotal((int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    /**
     * 后端 轮播图设置 使用 禁用 逻辑删除
     * @param cl
     * @return
     */
    @Override
    public ResponseResult UpClStatus(Carouselimage cl) {
        boolean b = this.updateById(cl);
        return b?ResponseResult.okResult("操作成功"):ResponseResult.errorResult(665,"操作失败");
    }

    @Resource
    private CarouselimageMapper carouselimageMapper;
    /**
     * 后台 添加 轮播图
     * @param cl
     * @return
     */
    @Override
    public ResponseResult saveCl(Carouselimage cl) {
        // 判断图片
        if (StringUtils.isBlank(cl.getImageUrl())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"图片不能为空");
        }
        cl.setStatus(1);
        cl.setPosition(1);
        carouselimageMapper.insert(cl);
        return ResponseResult.okResult("添加成功");
    }

    @Resource
    private FileStorageService fileStorageService;
    /**
     * 上传图片
     * @param file
     * @return
     */
    @Override
    @SneakyThrows
    public ResponseResult saveImage(MultipartFile file) {
        String s = fileStorageService.uploadImageFile(file.getName(), file.getInputStream());
        return ResponseResult.okResult(s);
    }




}




